<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { getApi } from "@/api/Inventory"
import { Search, Refresh, RefreshRight, CirclePlus } from "@element-plus/icons-vue"
import { type FormInstance } from "element-plus"
import { usePagination } from "@/hooks/usePagination"
import dayjs from "dayjs"
import { useRouter } from "vue-router"
import { TYPE_MAP, REBATE_MAP } from "./enums"
import { TYPE_MAP as COST_TYPE_MAP } from "@/components/FormAddItem/enum"

const router = useRouter()
defineOptions({
  // 命名当前组件
  name: "Inventory"
})
const searchData = reactive({
  product_code: "",
  warehousing_time: ""
})
const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()

//#region 查
const tableData = ref<any[]>([])

const getTableData = () => {
  loading.value = true
  if (searchData.warehousing_time) {
    searchData.warehousing_time = dayjs(searchData.warehousing_time).format("YYYY-MM-DD")
  }
  getApi({
    page: paginationData.page,
    size: paginationData.size,
    ...searchData
  })
    .then(({ data }: any) => {
      paginationData.total = data.pagination.total
      tableData.value = data.list
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}

//#endregion

/** 监听分页参数的变化 */
watch([() => paginationData.page, () => paginationData.size], getTableData, { immediate: true })

const searchFormRef = ref<FormInstance | null>(null)

const handleSearch = () => {
  paginationData.page === 1 ? getTableData() : (paginationData.page = 1)
}

const resetSearch = () => {
  searchFormRef.value?.resetFields()
  handleSearch()
}

const parse = (value: string) => {
  console.log(value, 111)

  return JSON.parse(value)
}
</script>

<template>
  <div class="app-container">
    <el-card shadow="never" class="search-wrapper">
      <el-collapse accordion>
        <el-collapse-item name="1">
          <template #title>导入数据</template>
          <div>
            <el-button type="primary" :icon="CirclePlus" @click="router.push(`/inventory/operate`)">录入库存</el-button>
            <el-tooltip content="刷新当前页">
              <el-button type="primary" :icon="RefreshRight" circle @click="getTableData" />
            </el-tooltip>
          </div>
        </el-collapse-item>
      </el-collapse>
    </el-card>
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="product_code" label="商品编号">
          <el-input v-model="searchData.product_code" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="warehousing_time" label="入库日期">
          <el-date-picker v-model="searchData.warehousing_time" placeholder="请选择" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="loading" shadow="never">
      <div class="table-wrapper">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="product_code" label="商品编码" fixed />
          <el-table-column prop="weight" label="重量" />
          <el-table-column prop="buying_price" label="进货价" />
          <el-table-column prop="payment_amount" label="打款金额" />
          <el-table-column prop="box_gauge" label="箱规" />
          <el-table-column prop="product_quantity" label="商品数量" />
          <el-table-column prop="brand_side" label="品牌方" />
          <el-table-column prop="supplier" label="供货方" />
          <el-table-column prop="whether_to_rebate" label="是否返利">
            <template #default="scope">
              {{ REBATE_MAP[scope.row.whether_to_rebate] }}
            </template>
          </el-table-column>
          <el-table-column prop="rebate_rate" label="返利比例" />
          <el-table-column prop="cost_params" label="费用参数">
            <template #default="scope">
              <div v-for="(item, index) in parse(scope.row.cost_params)" :key="index">
                {{ COST_TYPE_MAP[item.type] }} : {{ item.cost }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="warehousing_time" label="入库时间">
            <template #default="scope">
              {{ dayjs(scope.row.warehousing_time).format("YYYY-MM-DD") }}
            </template>
          </el-table-column>
          <el-table-column prop="source" label="来源">
            <template #default="scope">
              {{ TYPE_MAP[scope.row.source] }}
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.size"
          :currentPage="paginationData.page"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
